package net.yambal.android.secretary_alarm;

import java.util.Date;

import net.yambal.android.secretary_alarm.db.Alarm;
import net.yambal.android.secretary_alarm.db.AlarmsTable;
import android.app.Activity;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ActivityAlarmEditor extends Activity {
	private Alarm _alarm;
	private AlarmsTable _alarm_table;
	private TextView _hour_minute;
	private EditText _alarm_label;
	private CheckBox _alarm_day_evr;
	private CheckBox _alarm_day_mon;
	private CheckBox _alarm_day_tue;
	private CheckBox _alarm_day_wed;
	private CheckBox _alarm_day_thu;
	private CheckBox _alarm_day_fry;
	private CheckBox _alarm_day_sat;
	private CheckBox _alarm_day_sun;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_alarm_editor);
        _alarm_table = new AlarmsTable(this);
        
        Long id = -1L;
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
        	id = extras.getLong("id");
        	Log.d("sa", "onCreate@AlarmEditorActivity extras id " + id);
        }

        if(id >= 0){
        	_alarm = _alarm_table.get(id);
        	
        	if(_alarm == null){
        		_alarm = new Alarm(this);
        	}
        }else{
        	_alarm = new Alarm(this);
        }
        
        Log.d("sa", "onCreate@AlarmEditorActivity " + _alarm.id);
        
        
        // label
        _alarm_label = (EditText)findViewById(R.id.edittextAlarmLabel);
        if(_alarm.label != null){
        	_alarm_label.setText(_alarm.label);
        }else{
        	_alarm_label.setText("Unnamed Alarm");
        }
        
        // evr
        _alarm_day_evr = (CheckBox)findViewById(R.id.checkboxEveryday);
        _alarm_day_evr.setChecked(_alarm.isDayChecked(Alarm.EVERYDAY));
        _alarm_day_evr.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					_alarm_day_mon.setChecked(false);
					_alarm_day_tue.setChecked(false);
					_alarm_day_wed.setChecked(false);
					_alarm_day_thu.setChecked(false);
					_alarm_day_fry.setChecked(false);
				}else{
					_alarm_day_mon.setChecked(true);
					_alarm_day_tue.setChecked(true);
					_alarm_day_wed.setChecked(true);
					_alarm_day_thu.setChecked(true);
					_alarm_day_fry.setChecked(true);
				}
				_alarm_day_sat.setChecked(false);
				_alarm_day_sun.setChecked(false);
			}
		});
        
        // mon
        _alarm_day_mon = (CheckBox)findViewById(R.id.checkboxAlarmDayMon);
        _alarm_day_mon.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					_alarm_day_evr.setChecked(false);
				}
			}
		});
        
        // tue
        _alarm_day_tue = (CheckBox)findViewById(R.id.checkboxAlarmDayTue);
        _alarm_day_tue.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					_alarm_day_evr.setChecked(false);
				}
			}
		});
        
        // wed
        _alarm_day_wed = (CheckBox)findViewById(R.id.checkboxAlarmDayWed);
        _alarm_day_wed.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					_alarm_day_evr.setChecked(false);
				}
			}
		});
        
        // thu
        _alarm_day_thu = (CheckBox)findViewById(R.id.checkboxAlarmDayThu);
        _alarm_day_thu.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					_alarm_day_evr.setChecked(false);
				}
			}
		});
        
        // fry
        _alarm_day_fry = (CheckBox)findViewById(R.id.checkboxAlarmDayFri);
        _alarm_day_fry.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					_alarm_day_evr.setChecked(false);
				}
			}
		});
        
        // sat
        _alarm_day_sat = (CheckBox)findViewById(R.id.checkboxAlarmDaySat);
        _alarm_day_sat.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					_alarm_day_evr.setChecked(false);
				}
			}
		});
        
        // sun
        _alarm_day_sun = (CheckBox)findViewById(R.id.checkboxAlarmDaySun);
        _alarm_day_sun.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					_alarm_day_evr.setChecked(false);
				}
			}
		});
        
        if(!_alarm.isDayChecked(Alarm.EVERYDAY)){
	        _alarm_day_mon.setChecked(_alarm.isDayChecked(Alarm.MONDAY));
	        _alarm_day_tue.setChecked(_alarm.isDayChecked(Alarm.TUESDAY));
	        _alarm_day_wed.setChecked(_alarm.isDayChecked(Alarm.WEDNESDAY));
	        _alarm_day_thu.setChecked(_alarm.isDayChecked(Alarm.THURSDAY));
	        _alarm_day_fry.setChecked(_alarm.isDayChecked(Alarm.FRIDAY));
	        _alarm_day_sat.setChecked(_alarm.isDayChecked(Alarm.SATURDAY));
	        _alarm_day_sun.setChecked(_alarm.isDayChecked(Alarm.SUNDAY));
        }
        
        // Hour Minute
        if(_alarm.hour < 0 || _alarm.minute < 0){
        	Date now = new Date(System.currentTimeMillis());
        	_alarm.hour = now.getHours();
        	_alarm.minute = now.getMinutes();
        }
        Button btn_edit_hour_munute = (Button)findViewById(R.id.buttonEditHourMinute);
        btn_edit_hour_munute.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityAlarmEditor.this.editHourMinute();
			}
		});
        _hour_minute = (TextView)findViewById(R.id.textviewHourMinute);
        _hour_minute.setText(String.format("%02d:%02d", _alarm.hour, _alarm.minute));
        
        // save
        Button btn_save = (Button)findViewById(R.id.buttonSaveAlarm);
        btn_save.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityAlarmEditor.this.save();
			}
		});
        
        // delete
        Button btm_delete = (Button)findViewById(R.id.buttonDeleteAlarm);
        btm_delete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityAlarmEditor.this.delete();
			}
		});
        if(_alarm.id < 0){
        	btm_delete.setEnabled(false);
        	btm_delete.setClickable(false);
        }else{
        	btm_delete.setEnabled(true);
        	btm_delete.setClickable(true);
        }
	}
	
	private void editHourMinute(){
		new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener(){
			@Override
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
				_alarm.hour = hourOfDay;
				_alarm.minute = minute;
				_hour_minute.setText(String.format("%02d:%02d", _alarm.hour, _alarm.minute));
			};
		} , _alarm.hour, _alarm.minute, true).show();
	}
	
	private void save(){
		CharSequence str = _alarm_label.getText();
		_alarm.label = str.toString();
		
		// evr
		if(_alarm_day_evr.isChecked()){
			_alarm.setDay(Alarm.EVERYDAY);
		}else{
			_alarm.removeDay(Alarm.EVERYDAY);
		}
		
		// mon
		if(_alarm_day_mon.isChecked()){
			_alarm.setDay(Alarm.MONDAY);
		}else{
			_alarm.removeDay(Alarm.MONDAY);
		}
		// tue
		if(_alarm_day_tue.isChecked()){
			_alarm.setDay(Alarm.TUESDAY);
		}else{
			_alarm.removeDay(Alarm.TUESDAY);
		}
		// wed
		if(_alarm_day_wed.isChecked()){
			_alarm.setDay(Alarm.WEDNESDAY);
		}else{
			_alarm.removeDay(Alarm.WEDNESDAY);
		}
		// thu
		if(_alarm_day_thu.isChecked()){
			_alarm.setDay(Alarm.THURSDAY);
		}else{
			_alarm.removeDay(Alarm.THURSDAY);
		}
		// fry
		if(_alarm_day_fry.isChecked()){
			_alarm.setDay(Alarm.FRIDAY);
		}else{
			_alarm.removeDay(Alarm.FRIDAY);
		}
		// sat
		if(_alarm_day_sat.isChecked()){
			_alarm.setDay(Alarm.SATURDAY);
		}else{
			_alarm.removeDay(Alarm.SATURDAY);
		}
		// sut
		if(_alarm_day_sun.isChecked()){
			_alarm.setDay(Alarm.SUNDAY);
		}else{
			_alarm.removeDay(Alarm.SUNDAY);
		}

		_alarm_table.save(_alarm);
		
		/** TODO msg **/
		Intent intent = new Intent(this, ActivityAlarmList.class);
    	intent.putExtra("msg", _alarm.label + getString(R.string.massageDeleteAlarm));
    	startActivity(intent);
	}
	
	private void delete(){
		_alarm_table.delete(_alarm);
		
    	Intent intent = new Intent(this, ActivityAlarmList.class);
    	intent.putExtra("msg", _alarm.label + getString(R.string.massageDeleteAlarm));
    	startActivity(intent);
	}
	
	@Override
	public void onPause(){
		super.onPause();
	}
}
